package com.jingyanzi.channel.service.impl;

import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jingyanzi.channel.dao.ContactsMapper;
import com.jingyanzi.channel.domain.ContactsDO;
import com.jingyanzi.channel.entity.Contacts;
import com.jingyanzi.channel.entity.ContactsExample;
import com.jingyanzi.channel.entity.ContactsExample.Criteria;
import com.jingyanzi.channel.query.ContactsQueryDO;
import com.jingyanzi.channel.result.ResultDO;
import com.jingyanzi.channel.result.ResultSupport;
import com.jingyanzi.channel.service.IContactsService;
import com.jingyanzi.channel.util.BeanUtilsExtends;
import com.jingyanzi.sys.domain.DictionariesDO;
import com.jingyanzi.sys.domain.service.ISettingService;
import com.jingyanzi.sys.factory.SysFactory;

@Service("contactsservice")
public class ContactsServiceImpl extends BaseServiceImpl implements IContactsService {

	@Autowired
	private ContactsMapper contactsMapper;

	private ISettingService getSettingService() {
		return SysFactory.getISettingService();
	}

	@Override
	public ResultDO add(ContactsDO contactsDO) {
		Contacts contacts = new Contacts();
		com.jingyanzi.sys.domain.result.ResultDO resultDO = null;
		ResultSupport result = BeanUtilsExtends.copy(contacts, contactsDO);
		if (!result.isSuccess()) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "对象转换失败");
			result.setSuccess(false);
			return result;
		}
		contacts.setCreatetime(Calendar.getInstance().getTime());
		contacts.setIsDelete(1);
		int r = -1;

		try {
			if (contactsDO.getLevel() != null) {
				resultDO = getSettingService().getDictionariesById(contactsDO.getLevel());
				DictionariesDO dictionariess = (DictionariesDO) resultDO.getModel(ResultSupport.FIRST_MODEL_KEY);
				if (dictionariess == null) {
					result.setErrorCode(ResultDO.CONTACTS_ADD_ERROR);
					result.setErrorMsg("不存在该字典信息");
					result.setSuccess(false);
					return result;
				}
				contacts.setLevelName(dictionariess.getDataItem());
			}
			r = contactsMapper.insert(contacts);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "添加联系人失败");
			result.setSuccess(false);
			return result;
		}

		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "添加联系人失败");
			result.setSuccess(false);
			return result;
		}

		result.setModel(ResultDO.FIRST_MODEL_KEY, contacts.getId());
		return result;
	}

	@Override
	public ResultDO modifi(ContactsDO contactsDO) {
		Contacts contacts = new Contacts();
		com.jingyanzi.sys.domain.result.ResultDO resultDO = null;
		ResultSupport result = BeanUtilsExtends.copy(contacts, contactsDO);
		if (!result.isSuccess()) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "对象转换失败");
			result.setSuccess(false);
			return result;
		}
		int r = -1;

		try {
			if (contactsDO.getLevel() != null) {
				resultDO = getSettingService().getDictionariesById(contactsDO.getLevel());
				DictionariesDO dictionariess = (DictionariesDO) resultDO.getModel(ResultSupport.FIRST_MODEL_KEY);
				if (dictionariess == null) {
					result.setErrorCode(ResultDO.CONTACTS_MODIFI_ERROR);
					result.setErrorMsg("不存在该字典信息");
					result.setSuccess(false);
					return result;
				}
				contacts.setLevelName(dictionariess.getDataItem());
			}
			r = contactsMapper.updateByPrimaryKeySelective(contacts);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "修改联系人失败");
			result.setSuccess(false);
			return result;
		}

		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "修改联系人失败");
			result.setSuccess(false);
			return result;
		}

		result.setModel(ResultDO.FIRST_MODEL_KEY, contacts.getId());
		return result;
	}

	@Override
	public ResultDO remove(Integer[] ids) {
		Contacts contacts = new Contacts();

		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;
		for (int i = 0; i < ids.length; i++) {
			contacts.setId(ids[i]);
			contacts.setIsDelete(-1);

			int r = -1;

			try {
				r = contactsMapper.updateByPrimaryKeySelective(contacts);
			} catch (Exception e) {
				e.printStackTrace();
				result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除联系人失败");
				result.setSuccess(false);
				return result;
			}

			if (r < 1) {
				result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除联系人失败");
				result.setSuccess(false);
				return result;
			}
		}
		result.setModel(ResultDO.FIRST_MODEL_KEY, contacts.getId());
		return result;
	}

	@Override
	public ResultDO get(Integer id) {
		Contacts contacts = new Contacts();

		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;

		try {
			contacts = contactsMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取联系人失败");
			result.setSuccess(false);
			return result;
		}

		if (contacts == null) {
			result.setErrorCode(ResultDO.CONTACTS_GET_ERROR);
			result.setErrorMsg("不存在该联系人");
			result.setSuccess(false);
			return result;
		}
		ContactsDO contactsDO = getContactsDO(contacts);
		result.setModel(ResultDO.FIRST_MODEL_KEY, contactsDO);
		return result;
	}

	@Override
	public ResultDO query(ContactsQueryDO contactsQueryDO) {
		ResultSupport result = new ResultSupport();

		List<Contacts> channelwindows = null;
		List<ContactsDO> list = null;
		ContactsExample example = new ContactsExample();

		Criteria criterion = example.createCriteria();
		if (contactsQueryDO.getGender() != null) {
			criterion.andGenderEqualTo(contactsQueryDO.getGender());
		}

		if (contactsQueryDO.getLevel() != null) {
			criterion.andLevelEqualTo(contactsQueryDO.getLevel());
		}

		if (contactsQueryDO.getBirthday() != null) {
			criterion.andBirthdayEqualTo(contactsQueryDO.getBirthday());
		}
		if (contactsQueryDO.getChannelId() != null) {
			criterion.andChannelIdEqualTo(contactsQueryDO.getChannelId());
		}
		criterion.andIsDeleteEqualTo(1);
		if (contactsQueryDO.isPage()) {
			example.setLimitByPage(contactsQueryDO.getStart() + "," + contactsQueryDO.getPageRows());
			long count = contactsMapper.countByExample(example);
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
		}
		try {
			channelwindows = contactsMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取联系人失败");
			result.setSuccess(false);
			return result;
		}
		list = getContactsDOList(channelwindows);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, list);

		return result;
	}

}
